﻿layui.use(['form', 'layedit', 'transfer', 'util', 'layer', 'laydate', 'jquery'], function () {

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        transfer = layui.transfer,
        util = layui.util,
        $ = layui.jquery, player = parent.layer || layui.layer;

    var authorityMenusButtonsLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                queryData: undefined,
                loadButtonsUrl: "/Authority/Menus/LoadButtons",
                saveButtonsUrl: "/Authority/Menus/SaveButtons",
            };
            
            this.initPage();

            this.bindEvent();
        },
        //初始化列表
        initPage: function () {

            //数据格式解析
            transfer.render({
                elem: '#buttonsTransfer'
                , parseData: function (res) {
                    return {
                        "value": res.gCode, //数据值
                        "title": res.sName, //数据标题
                        "disabled": res.disabled,  //是否禁用
                        "checked": res.checked //是否选中
                    }
                },
                showSearch: true,
                title: ['未选按钮', '已选按钮'], 
                id: 'buttonsTransferKey' //定义唯一索引
            });

            //获取信息
            self.Parameters.queryData = GrdAssist.queryString("data");
            if (self.Parameters.queryData) {
                self.logicFunc.loadInfo();
            }
        },
        //绑定事件
        bindEvent: function () {

            //监听提交
            form.on('submit(confirmButton)', self.logicFunc.saveInfo);

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);


        },
        //逻辑方法
        logicFunc: {
            //获取修改的信息
            loadInfo: function () {
                var json = {
                    parameters: self.Parameters.queryData
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.loadButtonsUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            //实例重载
                            transfer.reload('buttonsTransferKey', {
                                data: result.Data.AllButtonList,
                                value: result.Data.SelectedButtonList
                            })
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },
            ///保存数据
            saveInfo: function () {
                var getData = transfer.getData('buttonsTransferKey'); //获取右侧数据
                var codeList = [];
                //以jQuery对象的方法调用，兼容性好;也可以用$(a)将a转化为jquery对象，然后以$(a).each(function(){})的形式调用,下面的方法同
                $.each(getData, function (index, val) {     
                    codeList.push(val.value);
                });
                var json = {
                    parameters: {
                        menuCode: self.Parameters.queryData,
                        buttonCodeList:codeList
                    }  
                }; 
                GrdLayUI.ajaxPost({
                    url: self.Parameters.saveButtonsUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            });

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },
            //取消
            close: function () {
                player.closeAll();
            }
        },
        //工具
        tools: {
        }
    };
    authorityMenusButtonsLogic.init();
});
